package io.coderyeah.org.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.coderyeah.basic.Result;
import io.coderyeah.org.domain.DictionaryDetail;
import io.coderyeah.org.domain.DictionaryType;
import io.coderyeah.org.dto.DictionaryTypeDTO;
import io.coderyeah.org.service.DictionaryDetailService;
import io.coderyeah.org.service.DictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/dicType")
@Api("字典类型接口")
public class DictionaryTypeController {
    @Autowired
    private DictionaryTypeService typeService;
    @Autowired
    private DictionaryDetailService detailService;

    @ApiOperation("查询所有类型/分页")
    @PostMapping("/list")
    public Result list(@RequestBody DictionaryTypeDTO dictionaryTypeDTO) {
        final Page<DictionaryType> page = new Page<>(dictionaryTypeDTO.getCurrentPage(), dictionaryTypeDTO.getPageSize());
        final LambdaQueryWrapper<DictionaryType> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(dictionaryTypeDTO.getName())) {
            wrapper.like(DictionaryType::getName, dictionaryTypeDTO.getName());
        }
        wrapper.or();
        if (StrUtil.isNotBlank(dictionaryTypeDTO.getSn())) {
            wrapper.like(DictionaryType::getSn, dictionaryTypeDTO.getSn());
        }
        final Page<DictionaryType> typePage = typeService.page(page, wrapper);
        return Result.success(typePage.getRecords(), typePage.getTotal());
    }

    @ApiOperation("添加/修改")
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody DictionaryTypeDTO typeDTO) {
        try {
            typeService.saveOrUpdate(BeanUtil.copyProperties(typeDTO, DictionaryType.class));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
        return Result.success("提交成功");
    }

    @ApiOperation("删除字典类型")
    @DeleteMapping("/del/{id}")
    public Result del(@PathVariable("id") Long id) {
        try {
            typeService.removeById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
        return Result.success("删除成功");
    }

    @ApiOperation("批量删除")
    @PatchMapping("/delByBatch")
    public Result delByBatch(@RequestBody DictionaryTypeDTO typeDTO) {
        try {
            typeService.removeByIds(Arrays.asList(typeDTO.getIds()));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
        return Result.success("批量删除成功");
    }

    @ApiOperation("通过类型ID查询详细内容")
    @GetMapping("{id}")
    public Result getDetailByTypeId(@PathVariable("id") Long id) {
        final LambdaQueryWrapper<DictionaryDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DictionaryDetail::getTypesId, id);
        final List<DictionaryDetail> details = detailService.list(wrapper);
        return Result.success(details);
    }

}
